var postCommentAjax,lastCommentPublishTime;

function sendComment(text){
	var param = new Object();
	param.commentText = text;
	param.itemID = selectedItem.id;
	postItemAjax = fire("POST", "Comments", param, postCommentListener);
}

function postCommentListener(){
	var obj = validResponse(postCommentAjax);
	if (obj != undefined ) {
		if(obj.result == true){
		getNewComments();
		}else{
		showError(obj.errorMessage);
		}
	}
}

function getNewComments(loadAll) {
	if(!isViewItemPage()){
		return;
	}
	
	var url = insertParam("Comments", "itemID", selectedItem.id);
	if(!loadAll){
	url = insertParam(url, "startFrom", lastCommentPublishTime);
	}
	//var url = addTimeToURL("Comments", lastCommentPublishTime);
	postCommentAjax = fire("GET", url, null, newCommentsListener);
}


function newCommentsListener(){
	var obj = validResponse(postCommentAjax);
	if (obj != undefined && obj.comments.length > 0) {
		lastCommentPublishTime = obj.comments[0].date;
		showNewComments(obj.comments);
	}
}

getNewComments();
setInterval(getNewComments, 5000);
